#include <linux/module.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/cdev.h>

#include <linux/sched.h>  /* current and everything */
#include <linux/kernel.h> /* printk() */
#include <linux/fs.h>     /* everything... */
#include <linux/types.h>  /* size_t */
#include <linux/wait.h>

static int sleepy_major = 0;
static int sleepy_minor = 0;

struct cdev *sleepy_cdev;	

module_param(sleepy_major, int, S_IRUGO);
module_param(sleepy_minor, int, S_IRUGO);

static DECLARE_WAIT_QUEUE_HEAD(wq);
static int flag = 0;

ssize_t sleepy_read (struct file *filp, char __user *buf, size_t count, loff_t *pos)
{
	printk(KERN_DEBUG "process %i (%s) going to sleep\n",
			current->pid, current->comm);
	wait_event_interruptible(wq, flag != 0);
	flag = 0;
	printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);
	return 0; /* EOF */
}

ssize_t sleepy_write (struct file *filp, const char __user *buf, size_t count,
		loff_t *pos)
{
	printk(KERN_DEBUG "process %i (%s) awakening the readers...\n",
			current->pid, current->comm);
	flag = 1;
	wake_up_interruptible(&wq);
	return count; /* succeed, to avoid retrial */
}


struct file_operations sleepy_fops = {
	.owner = THIS_MODULE,
	.read =  sleepy_read,
	.write = sleepy_write,
};

void sleepy_cleanup(void)
{
#if 0	
	unregister_chrdev(sleepy_major, "sleepy");
#endif
//#if 0
	dev_t devno = MKDEV(sleepy_major, sleepy_minor);
	/* Get rid of our char dev entries */
	if (sleepy_cdev) {
		cdev_del(sleepy_cdev);		
		kfree(sleepy_cdev);
	}
	/* cleanup_module is never called if registering failed */
	unregister_chrdev_region(devno,1);
//#endif
}

int sleepy_init(void)
{
#if 0
	int result;

	/*
	 * Register your major, and accept a dynamic number
	 */
	result = register_chrdev(sleepy_major, "sleepy", &sleepy_fops);
	if (result < 0)
		return result;
	if (sleepy_major == 0)
		sleepy_major = result; /* dynamic */
	return 0;
#endif
//#if 0
	int result,err;
	dev_t dev = 0;

	if (sleepy_major) {
		dev = MKDEV(sleepy_major, sleepy_minor);
		result = register_chrdev_region(dev, 1 , "sleepy");
	} else {
		result = alloc_chrdev_region(&dev, sleepy_minor , 1,
				"sleepy");
		sleepy_major = MAJOR(dev);
	}
	if (result < 0) {
		printk(KERN_WARNING "sleepy: can't get major %d\n", sleepy_major);
		return result;
	}

        /* 
	 * allocate the devices -- we can't have them static, as the number
	 * can be specified at load time
	 */

	//complete_cdev = cdev_alloc();	//cdev_alloc() and kmalloc()  are both ok!  use them as you want!
	sleepy_cdev = kmalloc( sizeof(struct cdev), GFP_KERNEL);

	if (!sleepy_cdev) {
		result = -ENOMEM;
		goto fail;  /* Make this more graceful */
	}
	
	cdev_init(sleepy_cdev, &sleepy_fops);

	sleepy_cdev->owner = THIS_MODULE;

	err = cdev_add (sleepy_cdev, dev, 1);
	/* Fail gracefully if need be */
	if (err)
		printk(KERN_NOTICE "Error %d adding sleepy_cdev", err);

	return 0; /* succeed */

  fail:
	sleepy_cleanup();
	return result;
//#endif
}

module_init(sleepy_init);
module_exit(sleepy_cleanup);

MODULE_LICENSE("Dual BSD/GPL");
